package com.qing;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuliao on 2017/8/15.
 */
public class MyServer {
    public static void main(String[] args){
        String str ="dog AND cat AND bear OR monkey NOT panda";
        String[] array = str.split("AND");
        List<String> andList = new ArrayList<>();
        List<String> orList = new ArrayList<>();
        List<String> notList = new ArrayList<>();

        while(str.length()>0){
            if(!str.startsWith("AND")&&!str.startsWith("OR")&&!str.startsWith("NOT")){
                String and = str.substring(0,str.indexOf(" "));
                andList.add(and);
                str = str.replaceFirst(and,"").trim();
            }else if(str.startsWith("AND")){
                str = str.replaceFirst("AND ","");
                int position = str.indexOf(" ");
                if(position!=-1){
                    String and = str.substring(0,position);
                    andList.add(and);
                    str = str.replaceFirst(and,"").trim();
                }else{
                    andList.add(str);
                    str = str.replaceFirst(str,"").trim();
                }
            }else if(str.startsWith("OR")){
                str = str.replaceFirst("OR ","");
                int position = str.indexOf(" ");
                if(position!=-1){
                    String or = str.substring(0,position);
                    orList.add(or);
                    str = str.replaceFirst(or,"").trim();
                }else{
                    orList.add(str);
                    str = str.replaceFirst(str,"").trim();
                }
            }else if(str.startsWith("NOT")){
                str = str.replaceFirst("NOT ","");
                int position = str.indexOf(" ");
                if(position!=-1){
                    String or = str.substring(0,position);
                    notList.add(or);
                    str = str.replaceFirst(or,"").trim();
                }else{
                    notList.add(str);
                    str = str.replaceFirst(str,"").trim();
                }
            }else{
                throw new IllegalArgumentException("非法的参数异常");
            }
        }


//        while (str.indexOf(" AND ")!=-1){
//            int position = str.indexOf(" AND ");
//            String temp = str.substring(position+5,str.indexOf(" ",position+5));
//            andList.add(temp);
//            str = str.replaceFirst(" AND "+temp,"");
//        }
//
//        while (str.indexOf(" OR ")!=-1){
//            int position = str.indexOf(" OR ");
//            String temp = str.substring(position+4,str.indexOf(" ",position+4));
//            orList.add(temp);
//            str = str.replaceFirst(" OR "+temp,"");
//        }
//

        for (String andStr : andList){
            printMessage(andStr);
        }
        printMessage(".......or.......");
        for (String orStr : orList){
            printMessage(orStr);
        }
        printMessage(".......not.......");
        for (String notStr : notList){
            printMessage(notStr);
        }











    }

    private static void printMessage(String msg){
        System.out.println(msg);
    }

}
